﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web.Mvc;
using Project.Module;
using Project.Module.Common;
using Project.Module.DAO;
using Project.Module.DAO.BL;
using Project.Module.DAO.CMSBL;
using Project.Module.Model;
using Project.Module.Model.CMS;
using Project.Engine.Control;
using Project.Engine.Utilities;

namespace Project.Module.View.CMS
{
    public class CMSSearchProductView : BasePageRazor
    {
        public string keywordName = String.Empty;
        public string keywordSupplier = String.Empty;
        public string keywordPrice = String.Empty;
        public string keywordQuantity = String.Empty;
        public string choice = "0";
        public string compare = "=";
        public List<ProductModel> listProduct { get; set; }
        public List<SupplierModel> listSupplier = null;
        private ProductBL productBl = new ProductBL();
        private SupplierBL supplierBl = new SupplierBL();

        protected override void InitializePage()
        {
            base.InitializePage();
            listSupplier = supplierBl.GetAllSupplier();

            if (Request["keywordName"] != null || Request["keywordSupplier"] != null ||
                Request["keywordPrice"] != null || Request["keywordQuantity"] != null)
            {
                keywordName = Request["keywordName"] != "  " ? SecurityUtil.RemoveSqlInjection(Request["keywordName"].ToString()) : String.Empty;
                keywordSupplier = Request["keywordSupplier"] != "  " ? SecurityUtil.RemoveSqlInjection(Request["keywordSupplier"].ToString()) : String.Empty;
                keywordPrice = Request["keywordPrice"] != "  " ? SecurityUtil.RemoveSqlInjection(Request["keywordPrice"].ToString()) : String.Empty;
                keywordQuantity = Request["keywordQuantity"] != "  " ? SecurityUtil.RemoveSqlInjection(Request["keywordQuantity"].ToString()) : String.Empty;

                if (keywordName == String.Empty && keywordSupplier == String.Empty &&
                    keywordPrice == String.Empty && keywordQuantity == String.Empty)
                {
                    listProduct= null;
                } else {
                    int supplierID = ConvertUtil.ToInt32(keywordSupplier);
                    int price = ConvertUtil.ToInt32(keywordPrice);
                    int quantity = ConvertUtil.ToInt32(keywordQuantity);

                    listProduct = productBl.Search(keywordName, supplierID, price, quantity);
                }
                

                if (listProduct != null)
                {
                    foreach (var item in listProduct)
                    {
                        item.SupplierName = supplierBl.GetSupplierByID(item.SupplierID);
                        item.CateName = ConvertUtil.GetCateNameByCateId(item.CatagoryId);
                    }
                }
            }
        }
    }
}
